OpaqueFunction
Table of Content
OpaqueFunction#
Action that executes a Python function.
demo#
- Execute shell script using
ExecuteProcessaction - Using
OpaqueFunctionto build shell script arguments - Convert
LaunchConfigurationto string usingcontext.perform_substitution
shell script to execute#
hello.zsh
#!/bin/zsh
echo $1 > /tmp/1
launch file#
opa
from launch import LaunchDescription, LaunchContext
from launch.actions import ExecuteProcess, DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch.actions import OpaqueFunction
SCRIPT_PATH = "/home/user/ros2_ws/src/pkg_launch_tutorial/scripts/hello.zsh"
ARG1="arg1"
def func_demo(context: LaunchContext, arg1: LaunchConfiguration):
value = context.perform_substitution(arg1)
if not value:
value = "default value"
run_script = ExecuteProcess(
cmd=[[SCRIPT_PATH, " ", f"'{value}'"]],
shell=True
)
return [run_script]
def generate_launch_description():
ld = LaunchDescription()
arg1 = LaunchConfiguration(ARG1)
arg1_arg = DeclareLaunchArgument(
ARG1, default_value="", description="arg1"
)
func_action = OpaqueFunction(function=func_demo, args=[arg1])
ld.add_action(arg1_arg)
ld.add_action(func_action)
return ld